<?php
/**
 * Created by PhpStorm.
 * User: lelouchms
 * Date: 2019/5/6
 * Time: 17:24
 */

namespace app\index\controller;


use app\backend\controller\BackendController;
use app\common\exception\AuthenticationException;
use app\common\exception\CustomerException;
use app\common\exception\LoginException;
use app\common\model\User;

class Base extends BackendController
{

    public function initialize()
    {
        parent::initialize();
        $controllerName = explode(".", $this->request->controller());
        $controllerName = strtolower($controllerName[count($controllerName) - 1]);
        $actionName = $this->request->action(true);
        $webUser = session("web_user");
        //  登陆验证
        if (empty($webUser) || !is_array($webUser)) {
            if (!in_array($actionName, $this->allow_action) && !in_array(strtolower($controllerName), $this->allow_controller)) {
                $token = $this->request->header("token", $this->request->param("token"));
                if (empty($token)) throw new LoginException(LoginException::LOGIN_NONE);
                $webUser = (new User())->findByToken($token);
                if (empty($webUser)) throw new LoginException(LoginException::LOGIN_NONE);
                session("web_user", $webUser);
            }
        }
        if (!empty($this->denyActions) && in_array($actionName, $this->denyActions)) {
            throw new AuthenticationException("无相关的访问权限");
        }
    }

    public function _empty()
    {
        throw new CustomerException("无效的地址");
    }

}